import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 刘国水
 * Date: 2025-04-15
 * Time: 22:27
 */
public class MyArray implements IList{

    private int[] elem;

    private int usedSize;

    MyArray() {
        this.elem = new int[Constant.DEFAULT_CAPACITY];
    }
    @Override
    public void add(int data) {
        if(isFul()) {
            gow();
        }
        this.elem[this.usedSize] = data;
        usedSize++;
    }

    private boolean isFul() {
        return this.elem.length == usedSize;
    }

    private void gow() {
        this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
    }

    @Override
    public void add(int pos, int data) {
        if(isEmpty()) {
            gow();
        }
        String ret = Constant.ADD_POS_ILLEGALITY + pos;
        checkPosAdd(pos,ret);
        if(pos == usedSize) {
            this.elem[pos] = data;
            usedSize++;
            return;
        }
        for (int i = usedSize-1; i >= pos ; i--) {
            elem[i+1] = elem[i];
        }
        elem[pos] = data;
        usedSize--;
    }

    private boolean isEmpty() {
        return this.elem.length == usedSize;
    }

    private void checkPosAdd(int pos,String msg) {
        if(pos < 0||pos > usedSize) {
            throw new PosListException(msg);
        }
    }

    private void checkPos(int pos,String msg) {
        if(pos < 0 || pos >usedSize) {
            throw new PosListException(msg);
        }
    }

    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if (toFind == elem[i]) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if (elem[i] == toFind) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public int get(int pos) {
        if (isEmpty()){
            throw new EmptyListException(Constant.EMPTY_LIST);
        }
        String ret = Constant.GET_POS_ILLEGALITY + pos;
        checkPos(pos,ret);
        return this.elem[pos];
    }

    @Override
    public void set(int pos, int value) {
        if (isEmpty()){
            throw new EmptyListException(Constant.EMPTY_LIST);
        }
        String ret = Constant.SET_POS_ILLEGALITY +pos;
        checkPos(pos,ret);
        this.elem[pos] = value;
    }

    @Override
    public void remove(int toRemove) {
        if (isEmpty()) {
            throw new EmptyListException(Constant.EMPTY_LIST);
        }
        int index = indexOf(toRemove);
        if(index == -1) {
            System.out.println("没有你要删除的数据...");
        }
        for (int i = index; i < usedSize-1 ; i++) {
            elem[i] = elem[i+1];
        }
        usedSize--;
    }

    @Override
    public int size() {
        return this.usedSize;
    }

    @Override
    public void clear() {
        for (int i = 0; i < usedSize; i++) {
            this.elem[i] = 0;
        }
        usedSize = 0;
    }

    @Override
    public void display() {
        for (int i = 0; i < usedSize; i++) {
            System.out.print(this.elem[i] +" ");
        }
        System.out.println();
    }
}
